package com.xxx.meter.ui.meter.reading.info;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.RadioGroup;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xxx.meter.R;
import com.xxx.meter.model.http.bean.MachineInfoBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MeterReadingInfoAdapter extends BaseMultiItemQuickAdapter<MachineInfoBean, BaseViewHolder> {

    @SuppressLint("UseSparseArrays")
    private Map<Integer, RadioGroup> map = new HashMap<>();

    MeterReadingInfoAdapter(@Nullable List<MachineInfoBean> data) {
        super(data);
        addItemType(MachineInfoBean.TEXT, R.layout.item_meter_info_input);
        addItemType(MachineInfoBean.SELECT, R.layout.item_meter_info_select);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final MachineInfoBean item) {
        helper.setText(R.id.item_meter_info_name, item.getName());
        switch (helper.getItemViewType()) {
            case MachineInfoBean.TEXT:
                helper.setText(R.id.item_meter_info_text, item.getData() + item.getCompany());
                break;
            case MachineInfoBean.SELECT:
                //做数据处理使用
                RadioGroup radioGroup = helper.getView(R.id.item_meter_info_select_radio);
                map.put(helper.getAdapterPosition(), radioGroup);

                helper.setText(R.id.item_meter_info_select_true, item.getItemoption1());
                helper.setText(R.id.item_meter_info_select_false, item.getItemoption2());

                helper.setChecked(R.id.item_meter_info_select_true, item.getData() == (double) 1);
                helper.setChecked(R.id.item_meter_info_select_false, item.getData() == (double) 0);
                break;
        }
    }

    @NonNull
    @Override
    public List<MachineInfoBean> getData() {
        List<MachineInfoBean> data = super.getData();
        for (int i = 0; i < data.size(); i++) {
            MachineInfoBean machineInfoBean = data.get(i);
            if (machineInfoBean.getItemType() == MachineInfoBean.SELECT) {
                RadioGroup radioGroup = map.get(i);
                if (radioGroup.getCheckedRadioButtonId() == R.id.item_meter_info_select_true) {
                    machineInfoBean.setData((double) 1);
                } else if (radioGroup.getCheckedRadioButtonId() == R.id.item_meter_info_select_false) {
                    machineInfoBean.setData((double) 0);
                } else {
                    machineInfoBean.setData((double) 0);
                }
                data.set(i, machineInfoBean);
            }
        }
        return data;
    }
}
